package com.mogician.len.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.mogician.cropper.view.CropImageView;
import com.mogician.cropper.view.CropListener;
import com.mogician.len.R;
import com.mogician.len.util.ImageAlterTool;
import com.mogician.len.util.PhotoUtil;

import java.io.IOException;

public class MainActivity extends AppCompatActivity
{
    public static final int TAKE_PHOTO = 1;
    public static final int CROP_PHOTO = 2;
    private ImageView picture;
    private Uri imageUri;
    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button captureBtn = findViewById(R.id.button_capture);
        Button webviewBtn = findViewById(R.id.to_web_view);
        Button restoreBtn = findViewById(R.id.button_restore);
        Button affineBtn = findViewById(R.id.button_affine);
        Button greyBtn = findViewById(R.id.button_grey);
        Button reverseBtn = findViewById(R.id.button_reverse);
        picture = findViewById(R.id.picture);

        captureBtn.setOnClickListener(v->
                imageUri = PhotoUtil.takePicture(MainActivity.this, TAKE_PHOTO));
        webviewBtn.setOnClickListener(v->
        {
            Intent intent = new Intent(MainActivity.this, WebActivity.class);
            startActivity(intent);
        });
        restoreBtn.setOnClickListener(v->
                picture.setImageBitmap(bitmap));
        affineBtn.setOnClickListener(v->
        {
            Intent intent = new Intent(MainActivity.this, CropActivity.class);
            intent.setData(imageUri);
//            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);//error
            startActivityForResult(intent, CROP_PHOTO);
        });
        greyBtn.setOnClickListener(v->
                picture.setImageBitmap(ImageAlterTool.instance.grey(bitmap)));
        reverseBtn.setOnClickListener(v->
                picture.setImageBitmap(ImageAlterTool.instance.reverse(bitmap)));

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        switch (requestCode)
        {
            case TAKE_PHOTO:
                if(resultCode == RESULT_OK)
                {
                    try
                    {
                        if (data != null)
                        {
                            // 这里是针对从文件中选图片的处理
                            Uri uriData = data.getData();
                            if (uriData != null)
                                imageUri = uriData;
                        }
                        //将拍摄的照片显示出来
                        bitmap = BitmapFactory.decodeStream(
                                getContentResolver().openInputStream(imageUri));
                        picture.setImageBitmap(bitmap);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            case CROP_PHOTO:
                if(resultCode == RESULT_OK)
                {
                    try
                    {
                        Uri uri = data.getData();
                        Bitmap crop = BitmapFactory.decodeStream(
                                getContentResolver().openInputStream(uri));
                        picture.setImageBitmap(crop);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            default:
                break;
        }
    }

}
